home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / src / depend.awk < prev    next >
Encoding:
AWK Script  |  1996-05-17  |  3.1 KB  |  164 lines

  1. #! /usr/bin/awk -f
  2. # This is an awk script which does dependencies. We do NOT want it to
  3. # recursively follow #include directives.
  4. # We only add to dependencies those files which are inside of the rootdir 
  5. # tree :)
  6.  
  7. #
  8. # Surely there is a more elegant way to see if a file exists.  Anyone know
  9. # what it is?
  10. #
  11. function fileExists(f,    TMP, dummy, result) {
  12.     if(result=FILEHASH[f]) {
  13.         if(result=="Yes") {
  14.             return "Yes"
  15.         } else {return ""}
  16.     }
  17.     ERRNO = getline dummy < f
  18.     if(ERRNO >= 0) {
  19.         close(f)
  20.         return FILEHASH[f]="Yes"
  21.     } else {
  22.         FILEHASH[f]="No"
  23.         return ""
  24.     }
  25. }
  26.  
  27. function Canonic(path) {
  28.     while (path ~ "/[^/]*/\\.\\./")
  29.         gsub("/[^/]*/\\.\\./","/",path)
  30.     return path
  31. }
  32.  
  33. BEGIN{
  34.     hasdep=0
  35.     objprefix=""
  36.     USEDC=0
  37.     if(dolib) {
  38.         # dolib = "libdirectory libname"
  39.         split(dolib, dlib)
  40.         I=0
  41.         rootdir=srcdir
  42.         sub("/$","",rootdir)
  43.         sub("/[^/]*$","",rootdir)
  44.         while (getline > 0) {
  45.         if ($0 ~ "OBJS") {
  46.             objs=$0
  47.         } else if ($0 ~ "^/.*\\.h:  \\\\$") {
  48.             sub(":  \\\\$","",$0)
  49.             USED[USEDC]=$0
  50.             ++USEDC
  51.         }
  52.         }
  53.         sub("^OBJS=[ ]*\"[ ]*","",objs)
  54.         sub("\"[ ]*","",objs)
  55.         split(objs, obj)
  56.         printf "%s: ", dlib[2]
  57.         sub("/$","", dlib[1])
  58.         objprefix=dlib[1]"/"
  59.         for (fname in obj) {
  60.         fullname=dlib[1]"/"obj[fname]
  61.         printf " \\\n   %s", fullname
  62.         sub("\\.o$",".c",obj[fname])
  63.         ARGV[ARGC]=obj[fname]
  64.         ++ARGC
  65.         }
  66.         printf "\n"
  67.     }
  68.     if(!hpath) {
  69.         print "hpath is not set"
  70.         exit 1
  71.     }
  72.     if(!srcdir) {
  73.         print "srcdir is not set"
  74.         exit 1
  75.     }
  76.     sub("[/ ]*$","",srcdir)
  77.     srcdir=srcdir"/"
  78.     sub("^\./$","",srcdir)
  79.     split(hpath, parray)
  80.     for(path in parray) {
  81.         sub("^-I","",parray[path])
  82.         sub("[/ ]*$","",parray[path])
  83.         parray[path]=Canonic(parray[path])
  84.     }
  85.     for(path in ARGV) {
  86.         USED[USEDC]=Canonic(srcdir""ARGV[path])
  87.         ++USEDC
  88.     }
  89. }
  90.  
  91. /^#[     ]*include[     ]*[<"][^     ]*[>"]/{
  92.     found=0
  93.     if(LASTFILE!=FILENAME) {
  94.         if (hasdep) {
  95.             print cmd
  96.             hasdep=0
  97.         }
  98.         cmd=""
  99.         LASTFILE=FILENAME
  100.         depname=FILENAME
  101.         relpath=FILENAME
  102.         sub("\\.c$",".o: ",depname)
  103.         if (depname==FILENAME) {
  104.             depname=srcdir""depname
  105.             depname=Canonic(depname)
  106.             cmd="\n\t@touch "depname
  107.         } else
  108.             depname=objprefix""depname
  109.         sub("\\.h$",".h: ",depname)
  110.         if(relpath ~ "^\\." ) {
  111.             sub("[^/]*$","",  relpath)
  112.             relpath=relpath"/"
  113.             sub("//","/",  relpath)
  114.         } else {
  115.             relpath=""
  116.         }
  117.     }
  118.     fname=$0
  119.     sub("^#[     ]*include[     ]*[<\"]","",fname)
  120.     sub("[>\"].*","",fname)
  121.     if(fileExists(relpath""fname)) {
  122.         found=1
  123.         if (!hasdep) {
  124.             printf "%s", depname
  125.             hasdep=1
  126.         }
  127.         fullname=Canonic(srcdir""relpath""fname)
  128.         printf " \\\n   %s", fullname
  129.         if(fname ~ "^\\." ) {
  130.             partname=relpath""fname
  131.             afound=0
  132.             for(name in USED) {
  133.             if (USED[name] == fullname) {
  134.                 afound=1
  135.                 break
  136.             }
  137.             }
  138.             if (!afound) {
  139.             ARGV[ARGC]=partname
  140.             ++ARGC
  141.             USED[USEDC]=fullname
  142.             ++USEDC
  143.             }
  144.         }
  145.     } else {
  146.         for(path in  parray) {
  147.             if(fileExists(parray[path]"/"fname)) {
  148.                 found=1
  149.                 if (!hasdep) {
  150.                     printf "%s", depname
  151.                     hasdep=1
  152.                 }
  153.                 printf " \\\n   %s", parray[path]"/"fname
  154.             }
  155.         }
  156.     }
  157. }
  158.  
  159. END{
  160.     if (hasdep) {
  161.         print cmd
  162.     }
  163. }
  164.